
import { _decorator, Sprite, Label, SpriteFrame, Color } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import Action3dManager, { ActionMngType } from '../../Script/Common/Action3dManager';
const { ccclass, property } = _decorator;

@ccclass('RankItem')
export class RankItem extends yyComponent {

    public init(data) {
        if (!!data) {
            this.setData(data);
        }
    }
    public reset() {
        this.stopAction();
    }
    protected stopAction() {
        Action3dManager.getMng(ActionMngType.UI).stopAllActions(this.node);
    }

    protected setData(data: { rank: number, name: string, score: number }) {
        this.setRankImg(data.rank);
        this.setRoleName(data.name);
        this.setScore(data.score);
        this.node.setPosition(0, this.getRankPosY(data.rank), 0);
        this.node.active = data.rank <= 5;
    }
    protected getRankPosY(rank: number) {
        return (0.5 - rank) * 54;
    }

    public changeRank(data: { rank: number, score: number }) {
        if (this.rank == data.rank && this.score == data.score) {
            return;
        }
        this.setScore(data.score);
        this.setRankImg(data.rank);
        if (data.rank > 5) {
            this.stopAction();
            this.node.setPosition(0, this.getRankPosY(data.rank), 0);
            this.node.active = false;
        } else {
            this.node.active = true;
            this.moveTo(this.getRankPosY(data.rank));
        }
    }
    protected moveTo(y: number) {
        let move = Action3dManager.moveTo(0.5, 0, y, 0);
        Action3dManager.getMng(ActionMngType.UI).runAction(this.node, move);
    }

    public setColor(color: Color) {
        this.roleName.color = color;
        this.scoreLabel.color = color;
    }


    @property(Sprite)
    protected rankImg: Sprite = null;
    protected rank: number = 1;
    @property([SpriteFrame])
    protected rankImgs: SpriteFrame[] = [];
    protected setRankImg(rank: number) {
        this.rank = rank;
        if (rank > 5) {
            this.rankImg.spriteFrame = null;
        } else {
            this.rankImg.spriteFrame = this.rankImgs[rank - 1];
        }
    }
    @property(Label)
    protected roleName: Label = null;
    protected setRoleName(name: string) {
        this.roleName.string = name;
    }
    @property(Label)
    protected scoreLabel: Label = null;
    public score: number = 0;
    protected setScore(score: number) {
        this.score = score;
        this.scoreLabel.string = score.toString();
    }


}
